package ruifight.member.controller;

import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import ruifight.member.model.Advertisement;
import ruifight.member.model.AdvertisementExample;
import ruifight.member.model.NoticeInfo;
import ruifight.member.service.AdvertisementService;
import ruifight.member.service.NoticeInfoService;
import ruifight.member.util.ObjUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/interface")
public class AdvertisementController {

	private static final Logger logger = LoggerFactory.getLogger(AdvertisementController.class);

	@Autowired
	public AdvertisementService advertisementService;

	/**
	 * 获取广告
	 * @return
	 */
	@RequestMapping("/getAdvertisement/{id}")
	@ResponseBody
	public Advertisement getAdvertisement(@PathVariable Long id) {
		Advertisement advertisement = advertisementService.selectByPrimaryKey(id);
		return advertisement;
	}

	/**
	 * 添加广告
	 * @param advertisement
	 * @return
	 */
	@RequestMapping("/addAdvertisement")
	@ResponseBody
	public  Map<String, Object> addAdvertisement(@RequestBody Advertisement advertisement) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			advertisement.setCreateDate(new Date());
			advertisementService.insertSelective(advertisement);
			map.put("state", 1);
			map.put("msg", "提交成功");
		} catch (Exception e) {
			map.put("state", 0);
			map.put("msg", "提交失败");
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 添加广告
	 * @param advertisement
	 * @return
	 */
	@RequestMapping("/updateAdvertisement")
	@ResponseBody
	public  Map<String, Object> updateAdvertisement(@RequestBody Advertisement advertisement) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			advertisement.setCreateDate(new Date());
			advertisementService.updateByPrimaryKeySelective(advertisement);
			map.put("state", 1);
			map.put("msg", "提交成功");
		} catch (Exception e) {
			map.put("state", 0);
			map.put("msg", "提交失败");
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 获取广告列表
	 * @return
	 */
	@RequestMapping("/getAdvertisementList")
	@ResponseBody
	public List<Advertisement> getAdvertisementList() {
		List<Advertisement> advertisementList = advertisementService.selectByExample(new AdvertisementExample());
		return advertisementList;
	}

	/**
	 * 删除广告
	 * @param paramMap
	 * @return
	 */
	@RequestMapping("/delAdvertisement")
	@ResponseBody
	public Map<String, Object> delAdvertisement(@RequestBody Map<String, String> paramMap){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			advertisementService.deleteByPrimaryKey(Long.parseLong(paramMap.get("advertisementId")));
			map.put("state",1);
			map.put("msg","删除成功");
		}catch (Exception e){
			map.put("state",0);
			map.put("msg","删除失败");
			e.printStackTrace();
		}
		return map;
	}
}
